<?php
/**
 * Created by PhpStorm.
 * User: admin
 * Date: 2016/9/9
 * Time: 15:35
 */
if (!defined('ACCESS')) {
    exit('Access denied.');
}

class OrderProduct extends DBBase
{

    private static $table_name = 'shop_cart';
    // 查询字段
    private static $columns = array('id', 'userid', 'order_sn', 'product_id', 'product_nums', 'iddel', 'createtime', 'modifytime');

    public static function getTableName()
    {
        return parent::$table_prefix . self::$table_name;
    }


    public static function getOrderById($id)
    {
        $condition = array('id' => $id);
        $db = self::__instance();
        $ret = $db->get(self::getTableName(), self::$columns, $condition);
        return $ret;
    }

    public static function getOrderByUId($uid)
    {
        $condition = array('userid' => $uid);
        $db = self::__instance();
        $ret = $db->get(self::getTableName(), self::$columns, $condition);
        return $ret;
    }

    public static function add($function_data)
    {
        if (!$function_data || !is_array($function_data)) {
            return false;
        }
        $db = self::__instance();
        $id = $db->insert(self::getTableName(), $function_data);
        return $id;
    }

    public static function delete($id)
    {
        if ($id) {
            $condition = array('id' => $id);
        } else {
            $condition = " WHERE iddel != '-1'";
        }

        $db = self::__instance();
        $result = $db->delete(self::getTableName(), $condition);
        return $result;
    }

    public static function updateConditon($data, $where)
    {
        $db = self::__instance();
        $result = $db->update(self::getTableName(), $data, $where);
        return $result;
    }

    //改
    public static function updateById($id, $data)
    {

        $condition = array('id' => $id);
        $db = self::__instance();
        $result = $db->update(self::getTableName(), $data, $condition);
        return $result;
    }

    public static function updateByUid($uid, $data)
    {
        $condition = array('userid' => $uid);
        $db = self::__instance();
        $result = $db->update(self::getTableName(), $data, $condition);
        return $result;
    }

    public static function getProdsBySn($sn)
    {
        $condition = array('order_sn' => $sn);
        $db = self::__instance();
        $result = $db->select(self::getTableName(), self::$columns, $condition);
        return $result;
    }

    public static function getAllBuyProds($uid)
    {

        $db = self::__instance();

        $sql = "select * from " . self::getTableName() . " where userid='$uid' and iddel = 1";

        $obj = $db->query($sql);

        if ($obj) {
            $list = $obj->fetchAll();
            if ($list) {
                return $list;
            }
        } else {
            return array();
        }
    }

    public static function getOrdersProds($uid)
    {
        $db = self::__instance();

        $sql = "select o.id as oid,o.product_id,o.product_nums,p.* from " . self::getTableName() . " o," . Products::getTableName() . " p where o.product_id = p.products_id and o.userid = '$uid' and o.iddel = 1";

        $obj = $db->query($sql);

        if ($obj) {
            $list = $obj->fetchAll();

            if ($list) {
                return $list;
            }
        } else {
            return array();
        }
    }

}